package com.asen.commons.core.base.vo;

import com.asen.commons.core.base.dto.QueryDto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 页面数据
 *
 * @author Gary
 * @version 1.0
 * @date 2018-11-30
 */
@Data
@NoArgsConstructor
public class DcPage<T> {

    @ApiModelProperty(value = "是否分页", position = 1)
    private Boolean isPage = false;

    @ApiModelProperty(value = "当前页码", position = 2, example = "1")
    private int pageNum = 1;

    @ApiModelProperty(value = "每页行数", position = 3, example = "10")
    private int pageSize;

    @ApiModelProperty(value = "当前记录", position = 4, example = "8")
    private int size;

    @ApiModelProperty(value = "总记录数", position = 5, example = "1000")
    private long total;

    @ApiModelProperty(value = "总页数", position = 6, example = "3")
    private int pages;

    @ApiModelProperty(value = "数据列表", position = 7)
    private List<T> list;

    public DcPage(List<T> list) {
        this.isPage = false;
        this.list = list;
        this.size = list == null ? 0 : list.size();
        this.pageNum = 1;
        this.pageSize = size;
        this.total = size;
        this.pages = 1;
    }

    public DcPage(List<T> list, int total, int pageNum, int pageSize) {
        this.isPage = true;
        this.list = list;
        this.size = list == null ? 0 : list.size();
        this.total = total;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.pages = size > 0 ? 1 : 0;
        if (pageSize > 0) {
            pages = (total / pageSize) + ((total % pageSize) > 0 ? 1 : 0);
        }
    }

    public DcPage(List<T> list, int total, QueryDto queryDto) {
        this.list = list;
        this.size = list == null ? 0 : list.size();
        this.total = total;
        this.isPage = queryDto.getIsPage();
        this.pageNum = queryDto.getPageNum();
        this.pageSize = queryDto.getPageSize();
        this.pages = size > 0 ? 1 : 0;
        if (pageSize > 0) {
            pages = (total / pageSize) + ((total % pageSize) > 0 ? 1 : 0);
        }
    }
}
